import "../css/stuadd.css"
import $ from "./jquery"
import "./401.js"


$(function () {

    //图片点击
    $("#img").click(function () {
        $("#file").click();
    })

    let filename;
    //
    $("#file").change(function () {
        let files = this.files;
        console.log(files[0]);
        let fd = new FormData();
        fd.append("file", files[0])
        //发送ajax
        $.ajax({
            url: "http://192.168.30.63:3000/stus/upload",
            type: "post",
            data: fd,
            processData: false,
            contentType: false,
            success(res) {
                filename = res.files.filename;
                $("#img").attr("src", 'http://192.168.30.63:3000/temp/' + filename);
            }
        })
    })

    getClass();
    //修改
    $(".xg").click(function () {
        if ($("#name").val().trim() == "") {
            alert("学生姓名不能为空");
            return;
        }
        if ($("#age").val().trim() == "") {
            alert("学生年龄不能为空");
            return;
        }
        let likes = [];
        $("input[name=like]:checked").each(function (index, item) {
            likes.push($(item).val());
        })
        console.log(JSON.stringify(likes));
        $.ajax({
            url: 'http://192.168.30.63:3000/stus/add',
            data: { name: $("#name").val(), age: $("#age").val(), like: JSON.stringify(likes), gender: $("input[name=gender]:checked").val(), birth: $("input[name=birth]").val(), clas_id: $(".classlist").val(), filename },
            dataType: "json",
            type: "post",
            success: function (data) {
                if (data.code == 200) {
                    alert("添加成功")
                } else {
                    alert("添加失败");
                }
            }
        })
    })
    //得到班级
    function getClass() {
        $.ajax({
            url: 'http://192.168.30.63:3000/clas/getClass',
            dataType: "json",
            type: "get",
            // beforeSend: function () {
            //     $("#load").show();
            // },
            success: function (data) {
                if (data.code == 200) {
                    console.log(data.result);
                    data.result.forEach(function (item, index) {
                        $(".classlist").append(`<option value="${item._id}" >${item.cname}</option>`)
                    })
                    // $(".classlist").val(cid);
                }
            }
        })
    }
});


